from Crypto.Cipher import Salsa20
def test_func1():
    key=b'0123456789012345'
    cipher=Salsa20.new(key)
    ciphertext=cipher.encrypt(b'The secret I want to send.')
    ciphertext+=cipher.encrypt(b'The second part of the secret.')
    print(cipher.nonce)

from Crypto.Hash import SHA256
def test_SHA256():
    h=SHA256.new()
    h.update('message'.encode())
    print(h.hexdigest())

from Crypto.PublicKey import RSA
def test_RSA():
    key_pair = RSA.generate(4096) # 4096是密钥位数
    #导出公钥
    open('public.pem','wb').write(key_pair.publickey().export_key())
    # 导出私钥
    open('private.pem','wb').write(key_pair.export_key('PEM'))
    

def test_salsa20():    
    # 流加密
    # 加密方
    plaintext=b'Hello World!'
    key=b'0123456789012345'
    cipher=Salsa20.new(key=key)
    msg=cipher.nonce+cipher.encrypt(plaintext)# 消息=随机数+密文

    # 解密方
    msg_nonce=msg[:8]   # 从msg中提取前8字节作为nonce
    ciphertext=msg[8:]  # 从msg中提取第9字节到末尾作为密文
    cipher=Salsa20.new(key=key,nonce=msg_nonce)
    plaintext=cipher.decrypt(ciphertext)
    print(plaintext)

from Crypto.Cipher import AES
def test_AES():
    # 加密方
    plaintext=b'Hello World!'
    key=b'0123456789012345'
    cipher=AES.new(key,AES.MODE_EAX)
    nonce=cipher.nonce
    ciphertext,tag=cipher.encrypt_and_digest(plaintext)


if __name__  == "__main__":
    test_salsa20()

